<?php
	//后台操作记录类
	class operate_log
	{
		var $database = null;

		function operate_log()
		{
			$this->database = load( 'database' );
		}

		//返回后台操作记录列表数据
		function all( $data )
		{
			$size = isset( $data['size'] ) && preg_match( '!^\d+$!', $data['size'] ) ? $data['size'] : 12;
			$page = isset( $data['page'] ) && preg_match( '!^\d+$!', $data['page'] ) ? $data['page'] : 1;
			$privilege_id = isset( $data['privilege_id'] ) && preg_match( '!^\d+$!', $data['privilege_id'] ) ? $data['privilege_id'] : '';
			$start = isset( $data['start'] ) ? strtotime( $data['start'] ) : '';
			$end = isset( $data['end'] ) ? strtotime( $data['end'] ) : '';
			$now = strtotime( date( 'Y-m-d' ) );

			$operate_table = $start != '' && $start > 0 && $start < $now ? 'operate_log' : 'operate_log';
			$order = isset( $data['order'] ) ? $data['order'] : $operate_table . '.ctime desc';
			$where = $privilege_id != '' ? ' and ' . $operate_table . '.privilege_id = ' . $privilege_id : '';
			if ( $start != '' && $end != '' && $start > 0 && $end > 0 ) $where .= ' and ' . $operate_table . '.ctime between ' . $start . ' and ' . ( $end + 86399 );
			$count = 'from ' . $operate_table . ', account, privilege where ' . $operate_table . '.account_id = account.id and ' . $operate_table . '.privilege_id = privilege.id' . $where;
			if ( isset( $data['username'] ) && $data['username'] != '' ) $count .= " and account.username like '%" . $data['username'] . "%'";
			$list = 'select ' . $operate_table . '.ip, ' . $operate_table . '.ctime, ' . $operate_table . '.memo, account.username, privilege.name as privilege_name ' . $count . ' order by ' . $order;

			$result = $this->database->split( $count, $list, $size, $page );
			return $result;
		}

		//添加一条新操作记录
		function add( $data )
		{
			$result = array( 'success' => false, 'message' => array() );
			$validation = load( 'validation' );
			$field = array();

			$field['account_id']['kind'] = '';
			$field['account_id']['type'] = 'num';
			$field['account_id']['min'] = '1';
			$field['account_id']['max'] = '16';
			$field['account_id']['info'] = '请输入操作人ID';

			$field['memo']['kind'] = '';
			$field['memo']['type'] = 'text';
			$field['memo']['min'] = '0';
			$field['memo']['max'] = '255';
			$field['memo']['info'] = '请输入操作备注';

			$field['privilege_id']['kind'] = '';
			$field['privilege_id']['type'] = 'num';
			$field['privilege_id']['min'] = '1';
			$field['privilege_id']['max'] = '16';
			$field['privilege_id']['info'] = '请输入操作ID';

			$check = $validation->validate( $field, $data );

			if ( count( $check ) == 0 )
			{
				$data['ctime'] = time();
				$data['ip'] = config( 'server.REMOTE_ADDR' );

				$this->database->start();
				$id = $this->database->add( 'operate_log', $data );

				if ( preg_match( '!^\d+$!', $id ) && $this->database->end() )
				{
					$result['success'] = true;
					$result['data']['id'] = $id;
				}
				else
				{
					$result['message'][] = '执行创建失败';
				}
			}
			else
			{
				$result['message'] = $check;
			}

			return $result;
		}
	}
?>